package view;


import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
import controller.Control;
import model.DIRECTION;
import model.Enemy;
import model.Model;
import model.MarioObject;
import model.Player;

public class Mario extends JFrame{
	

	private static final long serialVersionUID = 1L;
	private Menubar menubar;
	private Statusleiste statusleiste;
	private Spielfeld spielfeld;
	private Model model;
	private Control control;
	private SoundLoader sound;
	
	private ListenerModel listenerModel;
	
	public Mario(){
		this.setTitle("Mario");
		this.setBounds(200,200,806,576);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		this.menubar = new Menubar();
		this.setJMenuBar(this.menubar);
		this.sound= new SoundLoader("/res/sound/");
		
		statusleiste = new Statusleiste();

		this.add(statusleiste);

		this.addKeyListener(new KeyboardListener(this));

		this.setResizable(false);
		this.setVisible(true);

		//new Highscore(this);
		this.newGame();
		
	}
	
	private void newGame(){
		this.model =null;
		this.control = null;
		this.model = new Model();
		
		
		this.listenerModel = new ListenerModel(this);
		this.model.addModelChangeEventListener(this.listenerModel);
		
		//Pruefe ob Spilfeld existiert - statusleiste eventuell dassselbe
		if(this.spielfeld != null){
			this.getContentPane().remove(this.spielfeld); //loesche falss vorhanden
		}
		this.spielfeld = new Spielfeld(this);
		this.getContentPane().add(this.spielfeld);
		
		
		this.control = new Control(this.model);
		this.control.start();
		
		//this.pack();
		this.setVisible(true);
		
		sound.play("background");
		
		while(true){
				render();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}
	
	private long delta=0,last=0,fps=0;
	
	public void render(){

		computeDelta();
		this.setTitle("Mario - FPS: "+fps+" ");
		this.updateStatusLeiste();
		this.spielfeld.render();
		this.spielfeld.repaint();

		
	}
	
	private void computeDelta() {

		delta = System.nanoTime() - last;
		last = System.nanoTime();
		fps = ((long) 1e9)/delta;
		
	}
	
	public void enterPressed(){
		this.newGame();//spinnt		
	}
	
	public Model getModel(){
		return this.model;		
	}
	
	public void moveEventHandler(DIRECTION dir, boolean pressed){
		MarioObject[][][] level = model.getLevel();
		Player player = null;
		
		for(int x=0; x < level[3].length; x++){
			for( int y=0; y < level[3][x].length; y++){
				if(level[3][x][y] instanceof Player) player = (Player) level[3][x][y];
			}
		}

		switch(dir){
			case LEFT:
				player.setMovingLeft(pressed); 
				break;
			case RIGHT:
				player.setMovingRight(pressed);  
				break;
			case UP:
				player.setMovingUp(pressed);  
				break;
			case DOWN: 
				break;
			default:
				break;
		}
		
	}
	
	public void updateStatusLeiste(){
		this.statusleiste.setLives(model.getLivess());
		this.statusleiste.setCoins(model.getCoins());
		this.statusleiste.setScore(model.getScore());
		this.statusleiste.setTime(model.getTime());
	}
	
	public static void main(String[] args){
		
		new Mario();
	}

}
